<?php
require_once('TVB/VideoInfrastructure/Client/requires.php');
require_once('TVB/ImageInfrastructure/Client/requires.php');
class ChapterGetAction extends BaseAction 
{
	/**
	 * @isnull true
	 */
	public $vid;
	public $act;
	public $from;

	public $ut;
	public $deleted_chapters;
	public function execute(){
		if(count($this->_errors) > 0){
			return 'filter-error';
		}

		try
		{
			//chapter list by video id
			$chapter_list = array();
			$chapter = new chapter();
			$chapter_list = $chapter->getChapterList($this->vid);
			$this->setData("chapter_list", $chapter_list);

			//concurrent handle
			$concurrent_time = array();
			if (!empty($chapter_list))
			{
				foreach ($chapter_list as $chapter)
				{
					$concurrent_time[] = $chapter->modified_at;
				}
			}
			$this->setData('concurrent_time', json_encode($concurrent_time));

			//delete chapters
			$this->setData('deleted_chapters', $this->deleted_chapters);

			# add publish log
			$publish_log = (!empty($chapter_list)) ? publish_log::getPublishLog('chapter', $chapter_list[0]->id) : '';
			$this->setData("publishLog", $publish_log);

			//video detail
			$video = new video();
			$video_detail = $video->getDetails($this->vid);
            if(empty($video_detail))
            {
                $this->addError('Video Id Not Exist', array('0099', 'Video Not Exist'));
			    return 'filter-error';
            }
            if ($video_detail['vi_status'] == AC::VIDEO_STATUS_UPLOADED )
			{
				$json_param = json_decode($video_detail['video_params']);
				$vi_client = new VideoInfrastructureClient();
				$video_detail['download_url'] = $vi_client->getExportVideoUrlEasily($json_param->export_id);
			}
			else
			{
				$video_detail['download_url'] = -1;
			}

            if ($video_detail['ii_id']) {
                $img_client = new ImageInfrastructureClient();
                $video_detail['thumbnail_source_url'] = $img_client->GetSourceImageUrl($video_detail['ii_id']);
			}else{
				$video_detail['thumbnail_source_url'] = '';
			}

			$video_detail['new_video_id'] = '';

			switch($video_detail['vi_status']){
			case AC::VIDEO_STATUS_QUEUE:
				$video_detail['source_url'] = AC::PDES_FLASH_ASSETS_PROCESS;
				$video_detail['image_source_url'] = AC::PDES_FLASH_ASSETS_PROCESS;
				break;
			case AC::VIDEO_STATUS_WAITING:
				$video_detail['source_url'] = AC::PDES_FLASH_ASSETS_WAIT;
				$video_detail['image_source_url'] = AC::PDES_FLASH_ASSETS_WAIT;
				break;
			case AC::VIDEO_STATUS_FAILED:
				$video_detail['source_url'] =  AC::PDES_FLASH_ASSETS_FAILURE;
				$video_detail['image_source_url'] = AC::PDES_FLASH_ASSETS_FAILURE;
				break;
			}

			$this->setData("video", $video_detail);

			//programme detail
			$programme = new programme();
			$programme_detail = $programme->getDetails($video_detail['programme_id']);
			$programme_name = explode("\n\t",$programme_detail->mix_programme_name);
			$programme_detail->zh_programme_name = isset($programme_name[0]) ? $programme_name[0] : "";
			$programme_detail->en_programme_name = isset($programme_name[1]) ? $programme_name[1] : "";
			$this->setData("programme", $programme_detail);

			//episode detail 
			$episode = new episode();
			$episode_id = episode_video::getEpisodeID($this->vid);
			$episode_detail = $episode->getDetails($episode_id);
            
        	$episodeObjects  = $episode_detail->episode_language;
            $episodeArray   = Model::object2array($episodeObjects);  
            $zh_title  = isset($episodeArray[1]['title']) ? $episodeArray[1]['title']: ''; 
            $en_title  = isset($episodeArray[2]['title']) ? $episodeArray[2]['title']: '';

			$episode_detail->zh_title = isset($zh_title) ? $zh_title : '';
			$episode_detail->en_title = isset($en_title) ? $en_title : '';
			$this->setData("episode", $episode_detail);

			//allow tags
			$allow_tag = CommonUtils::get_allow_tags("chapter");
			$tag_type = new tag_type();
			$allow_tag_list = $tag_type->findByInSql($allow_tag);
			$tag_arr = array();
			foreach ($allow_tag_list as $tag)
			{
				$tag_arr[$tag->id] = array("id"=>$tag->id,"name"=>$tag->system_label,'restrict'=>$tag->restricted);
			}
			$this->setData("allow_tag", $tag_arr);

			$this->setData('from', $this->from);
			$this->setData('act',$this->act);
			$this->setData('type', 'chapter_marking');

			return 'succ';
		}
		catch(Exception $e)
		{
			error_log("chapter get: {$this->vid}" . var_export($e,1));

			$this->emptyData();
			return 'system-error';
		}
	}    
}
?>
